package com.bitter.job;

import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author thomas.he
 * @date 2022/6/9
 **/
@EnableScheduling
@Component
public class FirstJob {
    @Autowired
    private RedissonClient redissonClient;

    private String redisKey = "quartzTest";


    public void task(){
        RLock lock = redissonClient.getLock(redisKey);

        try {
            /**
             * 获取时间(如果没有获取到会在这段时间内持续获取锁)
             * 获取有效时间(持有锁的时间)
             * 计时单位
             **/
            if(lock.tryLock(2,3, TimeUnit.SECONDS)){
                System.out.println("任务1成功获取到锁");
            }else {
                System.out.println("任务1锁已存在");
            }
        } catch (Exception e){
            e.printStackTrace();
        }

    }
}
